package com.ruoyi.web.controller.smy;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.smy.MallGoodsAttribute;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.smy.IMallGoodsAttributeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping(value = "/smy/goodsAttribute")
public class MallGoodsAttributeController extends BaseController {
	@Resource
	private IMallGoodsAttributeService mallGoodsAttributeService;

	@PreAuthorize("@ss.hasPermi('smy:goodsAttribute:list')")
	@GetMapping("/list")
	public TableDataInfo list(MallGoodsAttribute mallGoodsAttribute) {
		startPage();
		List<MallGoodsAttribute> list = mallGoodsAttributeService.selectMallGoodsAttributeList(mallGoodsAttribute);
		return getDataTable(list);
	}

	@PreAuthorize("@ss.hasPermi('smy:goodsAttribute:export')")
	@Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, MallGoodsAttribute mallGoodsAttribute) {
		List<MallGoodsAttribute> list = mallGoodsAttributeService.selectMallGoodsAttributeList(mallGoodsAttribute);
		ExcelUtil<MallGoodsAttribute> util = new ExcelUtil<MallGoodsAttribute>(MallGoodsAttribute.class);
		util.exportExcel(response, list, "【请填写功能名称】数据");
	}

	@PreAuthorize("@ss.hasPermi('smy:goodsAttribute:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return AjaxResult.success(mallGoodsAttributeService.selectMallGoodsAttributeById(id));
	}

	@PreAuthorize("@ss.hasPermi('smy:goodsAttribute:add')")
	@Log(title = "新增商品规格", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody MallGoodsAttribute mallGoodsAttribute) {
		try {
			mallGoodsAttributeService.saveAttributeWithSubList(mallGoodsAttribute);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("新增商品规格异常:{}",e.getMessage());
			return AjaxResult.error(e.getMessage());
		}
		return AjaxResult.success();
	}

	@PreAuthorize("@ss.hasPermi('smy:goodsAttribute:edit')")
	@Log(title = "更新商品规格", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody MallGoodsAttribute mallGoodsAttribute) {
		try {
			mallGoodsAttributeService.saveAttributeWithSubList(mallGoodsAttribute);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("更新商品规格异常:{}",e.getMessage());
			return AjaxResult.error(e.getMessage());
		}
		return AjaxResult.success();
	}

	@PreAuthorize("@ss.hasPermi('smy:goodsAttribute:remove')")
	@Log(title = "删除商品规格", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		List<MallGoodsAttribute> removeList = CollectionUtil.newArrayList();
		try {
			for (Long id : ids) {
				MallGoodsAttribute attribute = mallGoodsAttributeService.selectMallGoodsAttributeById(id);
				if(Objects.nonNull(attribute)){
					List<MallGoodsAttribute> attributeList = mallGoodsAttributeService.getSubAttributeList(attribute);
					removeList.add(attribute);
					removeList.addAll(attributeList);
				}
			}
			if(CollectionUtil.isNotEmpty(removeList)){
				removeList.forEach(item->{
					item.setStatus(2L);
					mallGoodsAttributeService.updateMallGoodsAttribute(item);
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("删除商品规格异常:{}",e.getMessage());
			return AjaxResult.error(e.getMessage());
		}
		return AjaxResult.success();
	}

	/**
	 *  获取商品规格父级列表
	 * @Date 2022/10/26 14:35
	 * @author ZhanCheng.Li
	 * @param mallGoodsAttribute
	 * @return
	 */
	@PreAuthorize("@ss.hasPermi('smy:goodsAttribute:list')")
	@GetMapping("/parentList")
	public TableDataInfo parentList(MallGoodsAttribute mallGoodsAttribute) {
		startPage();
		MallGoodsAttribute queryDto = new MallGoodsAttribute();
		queryDto.setStatus(0L);
		List<MallGoodsAttribute> list = mallGoodsAttributeService.selectAttributeParentPage(mallGoodsAttribute);
		return getDataTable(list);
	}

	/**
	 *  查询商品规格内容
	 * @Date 2022/10/26 15:45
	 * @author ZhanCheng.Li
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/attributeInfo/{id}")
	public AjaxResult getAttributeInfo(@PathVariable("id") Long id) {
		MallGoodsAttribute attribute = mallGoodsAttributeService.selectMallGoodsAttributeById(id);
		if(Objects.nonNull(attribute)){
			List<MallGoodsAttribute> attributeList = mallGoodsAttributeService.getSubAttributeList(attribute);
			attribute.setGoodsAttributeList(attributeList);
		}
		return AjaxResult.success(attribute);
	}
}
